package com.by.ai.controller;

import com.by.ai.dto.LogAnalysisRequest;
import com.by.ai.dto.LogAnalysisResponse;
import com.by.ai.service.LogAnalysisService;
import com.by.log.Logger;
import com.by.log.LoggerFactory;
import com.by.rpc.annotation.ApiEndpoint;
import com.by.rpc.annotation.RpcBody;
import com.by.rpc.annotation.RpcParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 日志分析RPC服务端点
 * 提供与LogAnalysisController相同的日志分析功能，但通过RPC方式暴露
 */
@Service
@ApiEndpoint(serviceName = "miaosha-ai", version = "1.0.0", group = "default")
public class LogAnalysisRpcEndpoint {

    @Autowired
    LogAnalysisService logAnalysisService;

    @PostMapping("/analyze")
    public LogAnalysisResponse analyzeLog(@RpcBody(value = "request") LogAnalysisRequest request) {
        return logAnalysisService.analyzeLog(request);
    }

    @GetMapping("/request-count")
    public Object analyzeRequestCount(
            @RpcParam("startTime") LocalDateTime startTime,
            @RpcParam("endTime") LocalDateTime endTime,
            @RpcParam("serviceName") String serviceName) {
        return logAnalysisService.analyzeRequestCount(startTime, endTime, serviceName);
    }

    @GetMapping("/traffic")
    public Object analyzeTraffic(
            @RpcParam("startTime") LocalDateTime startTime,
            @RpcParam("endTime") LocalDateTime endTime,
            @RpcParam("serviceName") String serviceName) {
        return logAnalysisService.analyzeTraffic(startTime, endTime, serviceName);
    }

    @GetMapping("/errors")
    public Object analyzeErrors(
            @RpcParam("startTime") LocalDateTime startTime,
            @RpcParam("endTime") LocalDateTime endTime,
            @RpcParam("serviceName") String serviceName) {
        return logAnalysisService.analyzeErrors(startTime, endTime, serviceName);
    }

    @GetMapping("/anomalies")
    public List<String> detectAnomalies(
            @RpcParam("startTime") LocalDateTime startTime,
            @RpcParam("endTime") LocalDateTime endTime) {
        return logAnalysisService.detectAnomalies(startTime, endTime);
    }
}